<?php
session_start();
require_once '../config/database.php';
require_once '../includes/functions.php';

// Require admin access
requireAdmin();

$order_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if (!$order_id) {
    header('Location: orders.php');
    exit();
}

// Get order details
$order = getOrderById($pdo, $order_id);
$order_items = getOrderItems($pdo, $order_id);

if (!$order) {
    header('Location: orders.php');
    exit();
}

$page_title = "Order Details";
?>
<?php include '../includes/header.php'; ?>

<main style="padding: 2rem 0;">
    <div class="container">
        <div style="max-width: 1000px; margin: 0 auto;">
            <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 2rem;">
                <h1 style="color: #2c3e50;">Order Details</h1>
                <a href="orders.php" class="btn btn-outline">
                    <i class="fas fa-arrow-left"></i> Back to Orders
                </a>
            </div>

            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 2rem;">
                <!-- Order Information -->
                <div class="card">
                    <div class="card-header">
                        <h2>Order Information</h2>
                    </div>
                    
                    <div style="display: grid; gap: 1rem;">
                        <div>
                            <strong>Order Number:</strong> <?php echo htmlspecialchars($order['order_number']); ?>
                        </div>
                        <div>
                            <strong>Customer:</strong> <?php echo htmlspecialchars($order['first_name'] . ' ' . $order['last_name']); ?>
                        </div>
                        <div>
                            <strong>Email:</strong> <?php echo htmlspecialchars($order['email']); ?>
                        </div>
                        <div>
                            <strong>Total Amount:</strong> <?php echo formatPrice($order['total_amount']); ?>
                        </div>
                        <div>
                            <strong>Status:</strong> 
                            <span class="status-badge status-<?php echo $order['status']; ?>">
                                <?php echo ucfirst($order['status']); ?>
                            </span>
                        </div>
                        <div>
                            <strong>Order Date:</strong> <?php echo date('M j, Y g:i A', strtotime($order['created_at'])); ?>
                        </div>
                        <?php if ($order['payment_method']): ?>
                        <div>
                            <strong>Payment Method:</strong> <?php echo htmlspecialchars($order['payment_method']); ?>
                        </div>
                        <?php endif; ?>
                        <?php if ($order['notes']): ?>
                        <div>
                            <strong>Notes:</strong> <?php echo htmlspecialchars($order['notes']); ?>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>

                <!-- Customer Address -->
                <div class="card">
                    <div class="card-header">
                        <h2>Shipping Address</h2>
                    </div>
                    
                    <div style="white-space: pre-line;"><?php echo htmlspecialchars($order['shipping_address']); ?></div>
                </div>
            </div>

            <!-- Order Items -->
            <div class="card" style="margin-top: 2rem;">
                <div class="card-header">
                    <h2>Order Items</h2>
                </div>
                
                <?php if (empty($order_items)): ?>
                    <p style="text-align: center; padding: 2rem; color: #7f8c8d;">No items found for this order.</p>
                <?php else: ?>
                    <div class="table-responsive">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>Product</th>
                                    <th>Quantity</th>
                                    <th>Price</th>
                                    <th>Total</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($order_items as $item): ?>
                                <tr>
                                    <td>
                                        <div style="display: flex; align-items: center; gap: 1rem;">
                                            <?php 
                                            // Product image mapping with fallback
                                            $product_images = [
                                                'Serenity Candle' => 'Serenity Candle.jpg',
                                                'Velvet Oud Diffuser' => 'Velvet Oud Diffuser.jpg',
                                                'Monthly Aura Box' => 'Monthly Aura Box.jpg',
                                                'Ocean Breeze Candle' => 'Ocean Breeze Candle.jpg',
                                                'Sandalwood Essential Oil' => 'Sandalwood Essential Oil.jpg',
                                                'Luxury Gift Set' => 'Luxury Gift Set.jpg'
                                            ];
                                            
                                            $image_filename = $product_images[$item['name']] ?? '';
                                            ?>
                                            <div style="width: 50px; height: 50px; border-radius: 4px; overflow: hidden; border: 1px solid #ddd; background: #f8f9fa; display: flex; align-items: center; justify-content: center;">
                                                <?php if ($image_filename): ?>
                                                    <img src="/chuan/535g/assets/images/<?php echo htmlspecialchars($image_filename); ?>" 
                                                         alt="<?php echo htmlspecialchars($item['name']); ?>" 
                                                         style="width: 100%; height: 100%; object-fit: cover;"
                                                         loading="lazy"
                                                         onerror="this.style.display='none'; this.nextElementSibling.style.display='flex';">
                                                    <div style="display: none; width: 100%; height: 100%; align-items: center; justify-content: center; background: #e9ecef;">
                                                        <i class="fas fa-image" style="color: #6c757d; font-size: 18px;"></i>
                                                    </div>
                                                <?php else: ?>
                                                    <div style="width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; background: #e9ecef;">
                                                        <i class="fas fa-image" style="color: #6c757d; font-size: 18px;"></i>
                                                    </div>
                                                <?php endif; ?>
                                            </div>
                                            <div>
                                                <strong><?php echo htmlspecialchars($item['name']); ?></strong>
                                            </div>
                                        </div>
                                    </td>
                                    <td><?php echo $item['quantity']; ?></td>
                                    <td><?php echo formatPrice($item['price']); ?></td>
                                    <td><?php echo formatPrice($item['quantity'] * $item['price']); ?></td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                <?php endif; ?>
            </div>

            <!-- Actions -->
            <div style="margin-top: 2rem; text-align: center;">
                <a href="update_order_status.php?id=<?php echo $order['id']; ?>" class="btn btn-primary">
                    <i class="fas fa-edit"></i> Update Status
                </a>
            </div>
        </div>
    </div>
</main>

<?php include '../includes/footer.php'; ?>
